OPC Studio User's Guide and Reference
Installed Examples - Web - WebService1

A simple Web service using ASMX technology. Provides "Hello World" method to read a value of an OPC "Classic" item.

Service code-behind:

// $Header: $
// Copyright (c) CODE Consulting and Development, s.r.o., Plzen. All rights reserved.

// ReSharper disable ArrangeModifiersOrder

// WebService1: A simple Web service using ASMX technology. Provides "Hello World" method to read a value of an OPC
// "Classic" item. The web service can be tested using the integrated test facility.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

using System.Web.Services;
using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.DataAccess.Extensions;

namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : WebService
    {
        static Service1()
        {
            // Enable auto-subscribing optimization (not necessary), which can improve performance with repeated Read requests.
            Client.TryEnableAutoSubscribingOptimization();
        }

        // Use a shared client instance to allow for better optimization.
        static private readonly EasyDAClient Client = new EasyDAClient();

        [WebMethod]
        public string HelloWorld()
        {
            // The following call may throw an OpcException, which would be reported to the client.
            // Production-quality code may need to catch the OpcException and handle it differently.
            object value = Client.ReadItemValue("", "OPCLabs.KitServer.2", "Demo.Ramp");
            return value?.ToString() ?? "";
        }
    }
}
' WebService1: A simple Web service using ASMX technology. Provides "Hello World" method to read a value of an OPC item.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

Imports System.Web.Services
Imports OpcLabs.EasyOpc.DataAccess
Imports OpcLabs.EasyOpc.DataAccess.Extensions

''' <summary>
''' Summary description for Service1
''' </summary>
<WebService(Namespace:="http://tempuri.org/"), WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1), ComponentModel.ToolboxItem(False)> _
Public Class Service1
    Inherits WebService
    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    ' [System.Web.Script.Services.ScriptService]

    Shared Sub New()
        ' Enable auto-subscribing optimization (not necessary), which can improve performance with repeated Read requests.
        Client.TryEnableAutoSubscribingOptimization()
    End Sub

    ' Use a shared client instance to allow for better optimization.
    Shared ReadOnly Client As New EasyDAClient

    <WebMethod()> _
    Public Function HelloWorld() As String
        ' The following call may throw an OpcException, which would be reported to the client.
        ' Production-quality code may need to catch the OpcException And handle it differently.
        Dim value As Object = Client.ReadItemValue("", "OPCLabs.KitServer.2", "Demo.Ramp")
        Return If(value Is Nothing, "", value.ToString())
    End Function
End Class

 

See Also

Conceptual